Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Ipc
Imports System.IO



Public Class Main

    Public Repository As String = ""
    Public SelectedPolicyFile As String = ""
    Public SelectedPolicyFileFullPath As String = ""
    Dim objSelectedPolicy As New INIUtil
    Dim objRegistry As New RegistryUtil
    'Dim INIUtil As New INIUtil("")
    'Dim RegistryUtil As New RegistryUtil

    Private Sub MainListBox_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles MainListBox.SelectedIndexChanged
        SelectedPolicyFile = MainListBox.SelectedItem.ToString
        SelectedPolicyFileFullPath = Repository & "\" & SelectedPolicyFile
        PolicyConfiguration.Visible = True
        PolicyTabControl.Visible = True
        objSelectedPolicy.FileName = SelectedPolicyFileFullPath
        Me.Text = "PassFiltAdmin (" & SelectedPolicyFileFullPath & ")"
        MainStatusStripLabel.Text = "(" & SelectedPolicyFileFullPath & ")"
        PolicyName.Text = objSelectedPolicy.GetString("PassFiltPolicy", "name", "")
    End Sub

    Private Sub SetRepositoryToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles SetRepositoryToolStripMenuItem.Click

        'If Repository = "" Then
        Dim validRepositorySelected As Boolean = False
        Dim SetRepositoryResult As MsgBoxResult
        Do Until validRepositorySelected = True
            With SetRepository
                .ShowNewFolderButton = False
                SetRepositoryResult = .ShowDialog()
                Repository = .SelectedPath

            End With
            If SetRepositoryResult = MsgBoxResult.Cancel Then
                Exit Sub
            End If
            If File.Exists(Repository & "\MPFRepository.REP") = True Then
                validRepositorySelected = True
                If objRegistry.DoesKeyExist(RegistryUtil.RegistryPossibleRoots.HKEY_CURRENT_USER, "Software\PassFiltAdmin") = False Then
                    objRegistry.CreateKey(RegistryUtil.RegistryPossibleRoots.HKEY_CURRENT_USER, "Software\PassFiltAdmin")
                End If
                objRegistry.CreateValue(RegistryUtil.RegistryPossibleRoots.HKEY_CURRENT_USER, "Software\PassFiltAdmin", "Repository", Repository)
                Me.Text = "PassFiltAdmin (" & Repository & ")"
                MainStatusStrip.Visible = True
                MainStatusStripLabel.Text = "(" & Repository & ")"
            Else
                MsgBox("The selected folder """ & Repository & """ is not a valid repository.", MsgBoxStyle.Critical, "PassFiltAdmin")
            End If
        Loop
        'End If

        'MsgBox(Repository)
        'MainStatusStrip. = Repository

        If Directory.Exists(Repository) = True Then
            MainListBox.Items.Clear()
            MainListBox.Visible = True
            For Each File As FileInfo In New DirectoryInfo(Repository).GetFiles
                If File.Extension.ToUpper = ".MPF" Then
                    MainStatusStripLabel.Text = "Loading: " & File.Name

                    MainListBox.Items.Add(File.Name)
                End If
            Next
            Me.Text = "PassFiltAdmin (" & Repository & ")"
            MainStatusStrip.Visible = True
            MainStatusStripLabel.Text = "(" & Repository & ")"
        Else
            MsgBox("The selected folder """ & Repository & """ is not a valid repository.", MsgBoxStyle.Critical, "PassFiltAdmin")
            Repository = ""
            MainStatusStripLabel.Visible = True
            MainListBox.Visible = False
            Me.Text = "PassFiltAdmin (No Repository Selected)"
            MainStatusStrip.Visible = True
            MainStatusStripLabel.Text = "(No Repository Selected)"
        End If
    End Sub


    Private Sub LoadPolicySettings()


    End Sub



    Private Sub SetReposoitory_HelpRequest(sender As System.Object, e As System.EventArgs) Handles SetRepository.HelpRequest

    End Sub

    Private Sub Main_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'Dim PowerShellForm As New PowerShellForm()
        'PowerShellForm.ShowDialog(Me)


        If objRegistry.DoesKeyExist(RegistryUtil.RegistryPossibleRoots.HKEY_CURRENT_USER, "Software\PassFiltAdmin") = False Then
            objRegistry.CreateKey(RegistryUtil.RegistryPossibleRoots.HKEY_CURRENT_USER, "Software\PassFiltAdmin")
        End If


        Repository = objRegistry.QueryValue(RegistryUtil.RegistryPossibleRoots.HKEY_CURRENT_USER, "Software\PassFiltAdmin", "Repository", "")

        If Directory.Exists(Repository) = False Then Repository = ""


        If Repository = "" Then
            MainStatusStripLabel.Visible = True
            MainListBox.Visible = False
            Me.Text = "PassFiltAdmin (No Repository Selected)"
            MainStatusStrip.Visible = True
            MainStatusStripLabel.Text = "(No Repository Selected)"
        Else
            Me.Text = "PassFiltAdmin (" & Repository & ")"
            MainStatusStripLabel.Text = "(" & Repository & ")"
            MainListBox.Items.Clear()
            MainListBox.Visible = True
            For Each File As FileInfo In New DirectoryInfo(Repository).GetFiles
                If File.Extension.ToUpper = ".MPF" Then
                    MainStatusStripLabel.Text = "Loading: " & File.Name

                    MainListBox.Items.Add(File.Name)
                End If
            Next
            Me.Text = "PassFiltAdmin (" & Repository & ")"
            MainStatusStrip.Visible = True
            MainStatusStripLabel.Text = "(" & Repository & ")"
        End If

        'Me.Text = "PassFiltAdmin"


    End Sub

    Private Sub PowerShellAdminButton_Click(sender As System.Object, e As System.EventArgs) Handles PowerShellAdminButton.Click
        Dim PowerShellForm As New PowerShellForm()
        PowerShellForm.ShowDialog(Me)

    End Sub

    Private Sub CreatePolicyToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles CreatePolicyToolStripMenuItem.Click
        Dim CreateNewPolicyFom As New CreateNewPolicyForm()
        CreateNewPolicyFom.ShowDialog(Me)

    End Sub
End Class



